package com.example.sports_meet;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Calendar;
import java.util.Objects;

public class InformationActivity extends AppCompatActivity {
    private AlertDialog dlg;
    private TextView tv_user;
    private ImageView btn_user;

    private AlertDialog dlg_phone;
    private TextView tv_phone;
    private ImageView btn_phone;

    private AlertDialog dlg_per;
    private TextView tv_per;
    private ImageView btn_per;

    private AlertDialog dlg_sex;
    private TextView tv_sex, tv_account;
    private ImageView btn_sex;
    private int flag = 0;
    private int choice = 0;

    private TextView tv_birth;
    private ImageView btn_birth;
    private DatePickerDialog dlg_birth;
    private Button btn_exit;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_information);
        tv_user = findViewById(R.id.tv_user);
        btn_user = findViewById(R.id.btn_user);
        tv_phone = findViewById(R.id.tv_phone);
        btn_phone = findViewById(R.id.btn_phone);
        tv_per = findViewById(R.id.tv_per);
        btn_per = findViewById(R.id.btn_per);
        tv_sex = findViewById(R.id.tv_sex);
        btn_sex = findViewById(R.id.btn_sex);
        tv_birth = findViewById(R.id.tv_birth);
        btn_birth = findViewById(R.id.btn_birth);
        btn_exit = findViewById(R.id.btn_exit);
        tv_account = findViewById(R.id.tv_account);
        Listener listener = new Listener();
        btn_user.setOnClickListener(listener);
        btn_phone.setOnClickListener(listener);

        btn_per.setOnClickListener(listener);
        btn_sex.setOnClickListener(listener);
        btn_birth.setOnClickListener(listener);
        btn_exit.setOnClickListener(listener);
        Objects.requireNonNull(getSupportActionBar()).hide();

        //从SharedPreferences中获取账号
        SharedPreferences sharedPreferences = getSharedPreferences("spfRecord", Context.MODE_PRIVATE);
        String account = sharedPreferences.getString("account", "未登录");
        //设置TextView的文本为账号
        tv_account.setText(account);
        // 在 Activity 启动时显示保存的数据
        onResume();

    }

    class Listener implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.btn_user:
                    View dlgview = LayoutInflater.from(InformationActivity.this).inflate(R.layout.dialog_layout, null);
                    EditText etname = dlgview.findViewById(R.id.etname);
                    Button btnok = dlgview.findViewById(R.id.btnok);
                    btnok.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            tv_user.setText(etname.getText().toString());
                            dlg.dismiss();
                        }
                    });
                    dlg = new AlertDialog.Builder(InformationActivity.this)
                            .setView(dlgview)
                            .create();
                    dlg.show();
                    break;
                case R.id.btn_phone:
                    View dlgview_phone = LayoutInflater.from(InformationActivity.this).inflate(R.layout.dialog_layout, null);
                    EditText etname_phone = dlgview_phone.findViewById(R.id.etname);
                    Button btnok_phone = dlgview_phone.findViewById(R.id.btnok);
                    btnok_phone.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            tv_phone.setText(etname_phone.getText().toString());
                            dlg_phone.dismiss();
                        }
                    });
                    dlg_phone = new AlertDialog.Builder(InformationActivity.this)
                            .setView(dlgview_phone)
                            .create();
                    dlg_phone.show();
                    break;
                case R.id.btn_per:
                    View dlgview_per = LayoutInflater.from(InformationActivity.this).inflate(R.layout.dialog_layout, null);
                    EditText etname_per = dlgview_per.findViewById(R.id.etname);
                    Button btnok_per = dlgview_per.findViewById(R.id.btnok);
                    btnok_per.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            tv_per.setText(etname_per.getText().toString());
                            dlg_per.dismiss();
                        }
                    });
                    dlg_per = new AlertDialog.Builder(InformationActivity.this)
                            .setView(dlgview_per)
                            .create();
                    dlg_per.show();
                    break;

                case R.id.btn_sex:
                    String sex[] = {"女", "男"};
                    //flag=0;
                    choice = 0;
                    dlg_sex = new AlertDialog.Builder(InformationActivity.this)
                            .setTitle("请选择性别")
//                        .setMessage("this is a dialog")
                            //数组选项0：默认项的数组下标
                            .setSingleChoiceItems(sex, 0, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    Log.i("test", i + "");
                                    flag = 1;
                                    choice = i;
                                }
                            })
                            .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    if (flag == 0) {
                                        tv_sex.setText(sex[0]);
                                    } else {
                                        tv_sex.setText(sex[choice]);
                                    }
                                }
                            })
                            .create();
                    dlg_sex.show();
                    break;

                case R.id.btn_birth:
                    Calendar cal = Calendar.getInstance();
                    int year = cal.get(Calendar.YEAR);
                    int month = cal.get(Calendar.MONTH);
                    int day = cal.get(Calendar.DAY_OF_MONTH);
                    dlg_birth = new DatePickerDialog(InformationActivity.this, new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                            tv_birth.setText(i + "-" + (i1 + 1) + "-" + i2);
                        }
                    }, year, month, day);
                    dlg_birth.show();
                    break;
                case R.id.btn_exit:

                    String birth = tv_birth.getText().toString();
                    String Msex = tv_sex.getText().toString();
                    String Muse = tv_user.getText().toString();
                    String Mper = tv_per.getText().toString();
                    String Mphone = tv_phone.getText().toString();
                    SharedPreferences sharedPreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    String account = tv_account.getText().toString();//获取当前账号
                    editor.putString(account + "birth", birth);
                    editor.putString(account + "Msex", Msex);
                    editor.putString(account + "use", Muse);
                    editor.putString(account + "per", Mper);
                    editor.putString(account + "phone", Mphone);


                    editor.apply();
                    Toast.makeText(InformationActivity.this, "个人信息已保存", Toast.LENGTH_SHORT).show();
            }


        }
    }

    protected void onResume() {
        super.onResume();
        SharedPreferences sharedPreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
        String account = tv_account.getText().toString();
        String birth = sharedPreferences.getString(account + "birth", "");
        String Msex = sharedPreferences.getString(account + "Msex", "");
        String Muse = sharedPreferences.getString(account + "use", "");
        String Mper = sharedPreferences.getString(account + "per", "");
        String Mphone = sharedPreferences.getString(account + "phone", "");

        TextView nameTextView = findViewById(R.id.tv_user);
        TextView birthTextView = findViewById(R.id.tv_birth);
        TextView phoneTextView = findViewById(R.id.tv_phone);
        TextView sexTextView = findViewById(R.id.tv_sex);
        TextView perTextView = findViewById(R.id.tv_per);


        nameTextView.setText(Muse);
        birthTextView.setText(birth);
        phoneTextView.setText(Mphone);
        sexTextView.setText(Msex);
        perTextView.setText(Mper);


    }
}
